package com.zzld.modules.autoinn.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.abel533.entity.Example;
import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.Customer;
import com.zzld.modules.autoinn.entity.Facilitator;
import com.zzld.modules.autoinn.service.ICustomerService;
import com.zzld.modules.autoinn.service.IFacilitatorService;
import com.zzld.modules.common.dto.CommonJSON;

@Controller
@RequestMapping("/autoinn/facilitator")
public class FacilitatorController {

	@Autowired
	private IFacilitatorService facilitatorService;

	@Autowired
	private ICustomerService customerService;

	private String facilitatorurl = "/modules/autoinn/facilitator/facilitatorCru";
	private String facilitatorlisturl = "/modules/autoinn/facilitator/facilitatorList";

	@RequestMapping(method = RequestMethod.GET)
	private String li() {
		return facilitatorlisturl;
	}

	@RequestMapping(value = "/update/pro")
	public String update(Model model, HttpServletRequest request) {
		return facilitatorurl;
	}

	@RequestMapping(value = "update/pro/json", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON updtepro(HttpServletRequest request) {
		CommonJSON json = new CommonJSON();

		Customer customer = null;

		try {
			Example example = new Example(Customer.class);
			example.createCriteria().andEqualTo("phone_num",
					request.getParameter("phone"));
			customer = customerService.selectByExample(example).get(0);

			// 判断是否已经绑定过同一个门店信息
            Example example1 = new Example(Facilitator.class);
            example1.createCriteria().andEqualTo("wx_openid", customer.getOpenid())
            .andEqualTo("storeid",request.getParameter("storid"));
            if(facilitatorService.selectByExample(example1).size()>0){
            	json.setMessage("请勿重复绑定");
            	json.setSuccess(false);
            	return json;
            }

			Facilitator facilitator = new Facilitator();
			facilitator.setId(request.getParameter("id"));
			facilitator.setName(request.getParameter("name"));
			facilitator
					.setState(Integer.valueOf(request.getParameter("state")));
			facilitator.setType(Integer.valueOf(request.getParameter("type")));
			facilitator.setWx_openid(customer.getOpenid());
			facilitator.setStoreid(request.getParameter("storid"));
			facilitator.setPassword("e10adc3949ba59abbe56e057f20f883e");
			facilitator.setInnertime(new Date());

			if (StringUtil.isBlank(facilitator.getId())) {
				// 新增
				facilitator.setId(UuidUtil.uuid().toString());
				try {
					if (facilitatorService.save(facilitator) > 0) {
						json.setMessage(MessageUtils.ADD_SUCCESS);
						json.setSuccess(true);
					}
				} catch (Exception e1) {
					json.setMessage(MessageUtils.ADD_FAIL);
					json.setSuccess(false);
				}
			} else {
				// 修改
				try {
					if (facilitatorService
							.updateByPrimaryKeySelective(facilitator) > 0) {
						json.setMessage(MessageUtils.EDIT_SUCCESS);
						json.setSuccess(true);
					}
				} catch (Exception e2) {
					json.setMessage(MessageUtils.EDIT_WARING);
					json.setSuccess(false);
				}
			}

		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			json.setSuccess(false);
			json.setMessage(e.getMessage());
		}

		return json;
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public CommonJSON list(Model model, HttpServletRequest request) {
		CommonJSON json = new CommonJSON();

		Customer customer = null;
		try {
			Example example = new Example(Customer.class);
			example.createCriteria().andEqualTo("phone_num",
					request.getParameter("phone"));
			customer = customerService.selectByExample(example).get(0);
		} catch (Exception e) {
			customer = null;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("storeid", request.getParameter("storeid"));

		if (request.getParameter("phone") != null) {
			if (customer == null) {
				map.put("wx_openid",request.getParameter("phone"));
			}else{
				map.put("wx_openid",customer.getOpenid());
			}
		}

		map.put("sort", "innertime");
		map.put("order", request.getParameter("order"));
		map.put("startime", request.getParameter("startime"));
		map.put("endtime", request.getParameter("endtime"));

		String PageNum = request.getParameter("page") == null ? "0" : request
				.getParameter("page");

		String PageSize = request.getParameter("rows") == null ? "10" : request
				.getParameter("rows");

		PageInfo page = facilitatorService.list(map, Integer.parseInt(PageNum),
				Integer.parseInt(PageSize));

		json.setRows(page.getList());
		json.setTotal(page.getTotal());

		return json;
	}
	
	@RequestMapping(value="/update/pro/ser")
	@ResponseBody
	public JSONObject setbyse(HttpServletRequest request){
		JSONObject json  = new JSONObject();
		Facilitator  fa = null;
		Customer customer = null;
		 try {
			fa = facilitatorService.selectByPrimaryKey( request.getParameter("id").toString());
			Example example = new Example(Customer.class);
			example.createCriteria().andEqualTo("openid",
					fa.getWx_openid());
			customer = customerService.selectByExample(example).get(0);
		 } catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		 
		 Map<String,Object> map  = new HashMap<String,Object>();
		 map.put("id", fa.getId());
		 map.put("name", fa.getName());
		 map.put("state",fa.getState());
		 map.put("type", fa.getType());
		 map.put("storeid", fa.getStoreid());
		 map.put("phone", customer.getPhoneNum());
		 json.putAll(map);
		return json;
	}
	
	@RequestMapping(value="del",method=RequestMethod.POST)
	@ResponseBody
	public CommonJSON del(HttpServletRequest request){
		CommonJSON json = new CommonJSON();
		String id = request.getParameter("id");
		try{
			if(!StringUtil.isBlank(id)){
				if(facilitatorService.deleteByPrimaryKey(id)>0){
					json.setMessage(MessageUtils.DELETE_ALL_SUCCESS);
					json.setSuccess(true);
				}else{
					json.setSuccess(false);
					json.setMessage(MessageUtils.DELETE_WARNING);
				}
			}
		}catch(Exception e){
			json.setSuccess(false);
			json.setMessage(MessageUtils.DELETE_WARNING);
		}
		return json;
	}
}
